package com.koron.css2.newProjectCenter.impl.inforManag;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.query.PubUpStoreQueryBean;
import com.koron.css2.newProjectCenter.mapper.PubUpStoreMapper;
import com.koron.css2.newProjectCenter.vo.PubUpStoreListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.TreeBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.PingYin;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 材料仓库
 *
 * @author xs
 */
public class PubUpStoreList implements ServerInterface {

    @Override
    @SuppressWarnings("rawtypes")
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            // 将入参转换为QueryBean
            PubUpStoreQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), PubUpStoreQueryBean.class);

            // 获取Mapper实例
            PubUpStoreMapper mapper = factory.getMapper(PubUpStoreMapper.class);

            if (queryBean.getToDo() != null && queryBean.getToDo() == 2) {
                return updateOrSave(mapper, userInfo, queryBean);
            }
            if (queryBean.getToDo() != null && queryBean.getToDo() == 3) {
                return delete(mapper, queryBean);
            }
            // 设置分页参数
            PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
            // 执行SQL
            List<PubUpStoreListVO> list = mapper.selecList(queryBean);
            // 输出分页结果
            MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
            info.setData(new PageInfo<>(list));
            return info;
        } catch (Exception e) {
            factory.close(false);
            logger.error("查询失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
        }
    }

    private MessageBean<?> updateOrSave(PubUpStoreMapper thisMapper, UserInfoBean userInfo, PubUpStoreQueryBean queryBean) {
        if (StringUtils.isEmpty(queryBean.getId())) {
            queryBean.setCreateAccount(userInfo.getUserInfo().getAcount());
            queryBean.setCreateTime(new DateTime());
            queryBean.setCreateName(userInfo.getUserInfo().getName());
            queryBean.setTenantId(userInfo.getCurWaterCode());
            queryBean.setId(Tools.getObjectId());
        } else {
            queryBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
            queryBean.setUpdateTime(new DateTime());
            queryBean.setUpdateName(userInfo.getUserInfo().getName());
        }
        queryBean.setStoreCode(PingYin.getInstance().getSelling(queryBean.getStoreName(), true, "?"));
        int num = thisMapper.updateOrSave(queryBean);

        MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
        info.setData(num);
        return info;
    }

    private MessageBean<?> delete(PubUpStoreMapper thisMapper, PubUpStoreQueryBean queryBean) {
        if (StringUtils.isEmpty(queryBean.getId())) {
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "主键id不能为空", null);
        }
        // 判断仓库下是否有材料
        if (thisMapper.getCountByStoreId(queryBean.getId()) > 0) {
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "当前仓库下有材料，不可删除", null);
        }
        int num = thisMapper.delete(queryBean.getId());
        MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
        info.setData(num);
        return info;
    }

    /**
     * 递归查询下级目录树
     *
     * @param list
     * @param treeBean
     */
    private void recTree(List<PubUpStoreListVO> list, TreeBean treeBean) {
        for (PubUpStoreListVO bean : list) {
            // 如果parentId=父级id，，说明这是他的下级
            if (treeBean.getId().equals(bean.getParentId())) {
                TreeBean b = new TreeBean();
                b.setId(bean.getId() + "");
                b.setCode(bean.getBusinessCode());
                b.setParent(treeBean.getId());
                b.setName(bean.getBusinessName());
                b.setIsParent(false);
                treeBean.setIsParent(true);
                treeBean.getChildren().add(b);
                recTree(list, b); // 递归循环下级目录
            }
        }
    }
}
